import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;




public class FileReader {

	public static HashMap<String, Double> aboveSupport_1 = new HashMap<String, Double> ();

	public static void main (String args[]){
		if(args.length==2){

			Hashtable<String, Integer> support_1 = new Hashtable<String, Integer> ();
			double countLine=0;
			double minSupport= Double.parseDouble(args[1]);

			try{

				FileInputStream fstream = new FileInputStream(args[0]);

				DataInputStream in = new DataInputStream(fstream);
				BufferedReader br = new BufferedReader(new InputStreamReader(in));
				String strLine;

				while ((strLine = br.readLine()) != null)   {
					countLine++;
					//System.out.println (strLine);
					insertIntoHash(support_1, strLine);


				}
				//Close the input stream

				printHashTable(support_1,countLine,minSupport);
				System.out.println ();
				System.out.println ("Total Transactions : "+countLine);
				Entropy entropy = new Entropy();
				entropy.calculateEntropy(minSupport,aboveSupport_1);

				in.close();
			}catch (Exception e){
				System.err.println("Error: " + e.getMessage());
				e.printStackTrace();
			}

		}else{

			System.out.println("Usage--");
			System.out.println("First parameter -- Input file");
			System.out.println("Second parameter -- Minimum Support Threshold in Percentage");

		}
	}







	private static void printHashTable(Hashtable<String, Integer> support_1, double countLine, double minSupport) {
		int cntElements=0,minSupportCnt=0;
		Set  set = support_1.entrySet();
		Iterator it = set.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();

			double currSupport = (((Integer)entry.getValue()*100))/countLine;
			String keyitem=(String)entry.getKey();
			if (currSupport>=minSupport){

				System.out.println(keyitem + " : " + currSupport);
				minSupportCnt++;
				aboveSupport_1.put(keyitem, currSupport);

			}
			//System.out.println(entry.getKey() + " : " + ((entry.getValue())));
			cntElements++;

		}
		System.out.println ();
		System.out.println("Total Distict Items : "+ cntElements);
		System.out.println ();
		System.out.println("Total Items Exceeding Minimum Support ("+minSupport+"%) : "+ minSupportCnt);

	}



	private static void insertIntoHash(Hashtable<String, Integer>  support_1,String strLine) {
		String elements[] = strLine.split("\t");
		for(int i =0; i< elements.length;i++)
		{

			if( support_1.containsKey(elements[i]) ){

				Integer cnt=(Integer)support_1.get(elements[i]);
				cnt++;
				support_1.remove(elements[i]);
				support_1.put(elements[i], cnt);

			}else{

				support_1.put(elements[i], new Integer (1));	
			}
		}

	}



}
